/**
 * createTime: 2018/3/21 11:22
 * author: zhibin.zhao
 * description:
 */
const path = require('path');
const glob = require('glob');
const gulify = require('uglifyjs-webpack-plugin');    //压缩css
const htmlPlugin = require('html-webpack-plugin');
const extractTextPlugin = require('extract-text-webpack-plugin');
const purifyCSSPlugin = require('purifycss-webpack');
const entry = require('./webpack_config/entry_webpack');
const webpack = require('webpack');
const copyWebpackPlugin = require('copy-webpack-plugin');

/*console.log(encodeURIComponent(process.env.type));
if(process.env.type === 'dev'){
    var website = {
        publicPath: 'http://zhaozb.com:5544/'
    };
}else{
    var website = {
        publicPath: 'http://1.1.7.205:8089/'
    };
}*/

var website = {
    publicPath: 'http://1.1.7.205:8089/'
};
module.exports = {
    // devtool: 'eval-source-map',
    // entry: entry.path,
    entry: {
        entry: './src/entry.js',
        // entry2: './src/entry2.js'
        jquery: 'jquery',
        vue: 'vue'
    },  //入口
    output: {
        path: path.resolve(__dirname, 'dist'),
        // filename: 'bundle.js'
        filename: '[name].js',
        publicPath: website.publicPath
    }, //出口
    module: {
        rules: [{
            test: /\.css$/, //正则匹配.css
            // use: ['style-loader', 'css-loader']
            // loader: ['style-loader', 'css-loader']
            /*use: [{
                loader: 'style-loader'
            },{
                loader: 'css-loader'
            }]*/
            use: extractTextPlugin.extract({
                fallback: 'style-loader',
                // use: 'css-loader'
                use: [{
                    loader: 'css-loader', options: {importLoaders: 1}
                    }, 'postcss-loader']
            })
        },{
            test: /\.(png|jpg|gif)/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 50000,
                    outputPath: 'images/'   //图片生成目录
                }
            }]
        },{
            test: /\.(htm|html)$/i,
            use: ['html-withimg-loader']
        },{
            test: /\.less$/,
            use: extractTextPlugin.extract({
                use: [{
                    loader: 'css-loader'
                },{
                    loader: 'less-loader'
                }],
                fallback: 'style-loader'
            })
        },{
            test: /\.scss/,
            use: extractTextPlugin.extract({
                use: [{
                    loader: 'css-loader'
                },{
                    loader: 'sass-loader'
                }],
                fallback: 'style-loader'
            })
        },{
            test: /\.(jsx|js)$/,
            use: [{
                loader: 'babel-loader',
                options: {
                    presets: ['es2015', 'react']
                }
            }]
        }]
    },  //图片压缩、js、css转换
    plugins: [
        // new gulify(),
        new htmlPlugin({
            minify: {   //压缩
                removeAttributeQuotes: true
            },
            hash: true, //防止缓存
            template: './src/index.html'
        }),
        new extractTextPlugin('css/index.css'),
        new purifyCSSPlugin({
            paths: glob.sync(path.join(__dirname, 'src/*.html'))    //搜索所有html文件，然后匹配css去掉没用的class
        }),
        new webpack.ProvidePlugin({
            $: 'jquery'
        }),
        new webpack.BannerPlugin('zhaozb版权所有'),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['jquery', 'vue'],
            filename: 'assets/js/[name].js',
            minChunks: 2
        }),
        new copyWebpackPlugin([{
            from: __dirname + '/src/public',
            to: './public'
        }])/*,
        new webpack.HotModuleReplacementPlugin()*/
    ], //插件
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        host: '1.1.7.205',
        compress: true,
        port: 8089
    },   //webpack开发服务
    watchOptions: {
        poll: 1000, //1秒检测一次
        aggregeateTimeout: 500, // 防止重复按键，例如重复Ctrl+S
        ignored: /node_modules/  //不检测某个文件夹
    }
}